

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.scipy.optimize.line_search &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.scipy.optimize.line_search</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.scipy.optimize.line_search</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2021 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;line search&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">NamedTuple</span>

<span class="kn">from</span> <span class="nn">...</span> <span class="kn">import</span> <span class="n">nn</span>
<span class="kn">from</span> <span class="nn">...</span> <span class="kn">import</span> <span class="n">numpy</span> <span class="k">as</span> <span class="n">mnp</span>
<span class="kn">from</span> <span class="nn">...common</span> <span class="kn">import</span> <span class="n">dtype</span> <span class="k">as</span> <span class="n">mstype</span>
<span class="kn">from</span> <span class="nn">...common</span> <span class="kn">import</span> <span class="n">Tensor</span>

<span class="kn">from</span> <span class="nn">..utils</span> <span class="kn">import</span> <span class="n">_to_scalar</span><span class="p">,</span> <span class="n">grad</span>
<span class="kn">from</span> <span class="nn">..utils</span> <span class="kn">import</span> <span class="n">_to_tensor</span><span class="p">,</span> <span class="n">_INT_ZERO</span><span class="p">,</span> <span class="n">_INT_ONE</span><span class="p">,</span> <span class="n">_BOOL_FALSE</span>


<span class="k">class</span> <span class="nc">_LineSearchResults</span><span class="p">(</span><span class="n">NamedTuple</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Results of line search results.</span>

<span class="sd">    Args:</span>
<span class="sd">        failed (bool): `True`` if the strong Wolfe criteria were satisfied</span>
<span class="sd">        nit (int): number of iterations</span>
<span class="sd">        nfev (int): number of functions evaluations</span>
<span class="sd">        ngev (int): number of gradients evaluations</span>
<span class="sd">        k (int): number of iterations</span>
<span class="sd">        a_k (float): step size</span>
<span class="sd">        f_k (float): final function value</span>
<span class="sd">        g_k (Tensor): final gradient value</span>
<span class="sd">        status (int): end status</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">failed</span><span class="p">:</span> <span class="nb">bool</span>
    <span class="n">nit</span><span class="p">:</span> <span class="nb">int</span>
    <span class="n">nfev</span><span class="p">:</span> <span class="nb">int</span>
    <span class="n">ngev</span><span class="p">:</span> <span class="nb">int</span>
    <span class="n">k</span><span class="p">:</span> <span class="nb">int</span>
    <span class="n">a_k</span><span class="p">:</span> <span class="nb">float</span>
    <span class="n">f_k</span><span class="p">:</span> <span class="nb">float</span>
    <span class="n">g_k</span><span class="p">:</span> <span class="n">Tensor</span>
    <span class="n">status</span><span class="p">:</span> <span class="nb">int</span>


<span class="k">def</span> <span class="nf">_cubicmin</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">fa</span><span class="p">,</span> <span class="n">fpa</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">fb</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">fc</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Finds the minimizer for a cubic polynomial that goes through the</span>
<span class="sd">    points (a,fa), (b,fb), and (c,fc) with derivative at a of fpa.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">C</span> <span class="o">=</span> <span class="n">fpa</span>
    <span class="n">db</span> <span class="o">=</span> <span class="n">b</span> <span class="o">-</span> <span class="n">a</span>
    <span class="n">dc</span> <span class="o">=</span> <span class="n">c</span> <span class="o">-</span> <span class="n">a</span>
    <span class="n">denom</span> <span class="o">=</span> <span class="p">(</span><span class="n">db</span> <span class="o">*</span> <span class="n">dc</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">db</span> <span class="o">-</span> <span class="n">dc</span><span class="p">)</span>

    <span class="n">d1</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
    <span class="n">d1</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">dc</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="n">d1</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">db</span> <span class="o">**</span> <span class="mi">2</span>
    <span class="n">d1</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="o">-</span><span class="n">dc</span> <span class="o">**</span> <span class="mi">3</span>
    <span class="n">d1</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">db</span> <span class="o">**</span> <span class="mi">3</span>

    <span class="n">d2</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,))</span>
    <span class="n">d2</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">fb</span> <span class="o">-</span> <span class="n">fa</span> <span class="o">-</span> <span class="n">C</span> <span class="o">*</span> <span class="n">db</span>
    <span class="n">d2</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">fc</span> <span class="o">-</span> <span class="n">fa</span> <span class="o">-</span> <span class="n">C</span> <span class="o">*</span> <span class="n">dc</span>

    <span class="n">A</span><span class="p">,</span> <span class="n">B</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">d1</span><span class="p">,</span> <span class="n">d2</span><span class="o">.</span><span class="n">flatten</span><span class="p">())</span> <span class="o">/</span> <span class="n">denom</span>

    <span class="n">radical</span> <span class="o">=</span> <span class="n">B</span> <span class="o">*</span> <span class="n">B</span> <span class="o">-</span> <span class="mf">3.</span> <span class="o">*</span> <span class="n">A</span> <span class="o">*</span> <span class="n">C</span>
    <span class="n">xmin</span> <span class="o">=</span> <span class="n">a</span> <span class="o">+</span> <span class="p">(</span><span class="o">-</span><span class="n">B</span> <span class="o">+</span> <span class="n">mnp</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">radical</span><span class="p">))</span> <span class="o">/</span> <span class="p">(</span><span class="mf">3.</span> <span class="o">*</span> <span class="n">A</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">xmin</span>


<span class="k">def</span> <span class="nf">_quadmin</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">fa</span><span class="p">,</span> <span class="n">fpa</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">fb</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Finds the minimizer for a quadratic polynomial that goes through</span>
<span class="sd">    the points (a,fa), (b,fb) with derivative at a of fpa.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">D</span> <span class="o">=</span> <span class="n">fa</span>
    <span class="n">C</span> <span class="o">=</span> <span class="n">fpa</span>
    <span class="n">db</span> <span class="o">=</span> <span class="n">b</span> <span class="o">-</span> <span class="n">a</span>
    <span class="n">B</span> <span class="o">=</span> <span class="p">(</span><span class="n">fb</span> <span class="o">-</span> <span class="n">D</span> <span class="o">-</span> <span class="n">C</span> <span class="o">*</span> <span class="n">db</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">db</span> <span class="o">**</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">xmin</span> <span class="o">=</span> <span class="n">a</span> <span class="o">-</span> <span class="n">C</span> <span class="o">/</span> <span class="p">(</span><span class="mf">2.</span> <span class="o">*</span> <span class="n">B</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">xmin</span>


<span class="k">def</span> <span class="nf">_zoom</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="n">a_low</span><span class="p">,</span> <span class="n">phi_low</span><span class="p">,</span> <span class="n">dphi_low</span><span class="p">,</span> <span class="n">a_high</span><span class="p">,</span> <span class="n">phi_high</span><span class="p">,</span> <span class="n">dphi_high</span><span class="p">,</span> <span class="n">phi_0</span><span class="p">,</span> <span class="n">g_0</span><span class="p">,</span> <span class="n">dphi_0</span><span class="p">,</span> <span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">is_run</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Implementation of zoom algorithm.</span>
<span class="sd">    Algorithm 3.6 from Wright and Nocedal, &#39;Numerical Optimization&#39;, 1999, pg. 59-61.</span>
<span class="sd">    Tries cubic, quadratic, and bisection methods of zooming.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_FLOAT_ONE</span> <span class="o">=</span> <span class="n">_to_tensor</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">a_low</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
    <span class="n">state</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s2">&quot;done&quot;</span><span class="p">:</span> <span class="n">_BOOL_FALSE</span><span class="p">,</span>
        <span class="s2">&quot;failed&quot;</span><span class="p">:</span> <span class="n">_BOOL_FALSE</span><span class="p">,</span>
        <span class="s2">&quot;j&quot;</span><span class="p">:</span> <span class="n">_INT_ZERO</span><span class="p">,</span>
        <span class="s2">&quot;a_low&quot;</span><span class="p">:</span> <span class="n">a_low</span><span class="p">,</span>
        <span class="s2">&quot;phi_low&quot;</span><span class="p">:</span> <span class="n">phi_low</span><span class="p">,</span>
        <span class="s2">&quot;dphi_low&quot;</span><span class="p">:</span> <span class="n">dphi_low</span><span class="p">,</span>
        <span class="s2">&quot;a_high&quot;</span><span class="p">:</span> <span class="n">a_high</span><span class="p">,</span>
        <span class="s2">&quot;phi_high&quot;</span><span class="p">:</span> <span class="n">phi_high</span><span class="p">,</span>
        <span class="s2">&quot;dphi_high&quot;</span><span class="p">:</span> <span class="n">dphi_high</span><span class="p">,</span>
        <span class="s2">&quot;a_rec&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">a_low</span> <span class="o">+</span> <span class="n">a_high</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">,</span>
        <span class="s2">&quot;phi_rec&quot;</span><span class="p">:</span> <span class="p">(</span><span class="n">phi_low</span> <span class="o">+</span> <span class="n">phi_high</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">,</span>
        <span class="s2">&quot;a_star&quot;</span><span class="p">:</span> <span class="n">_FLOAT_ONE</span><span class="p">,</span>
        <span class="s2">&quot;phi_star&quot;</span><span class="p">:</span> <span class="n">phi_low</span><span class="p">,</span>
        <span class="s2">&quot;dphi_star&quot;</span><span class="p">:</span> <span class="n">dphi_low</span><span class="p">,</span>
        <span class="s2">&quot;g_star&quot;</span><span class="p">:</span> <span class="n">g_0</span><span class="p">,</span>
        <span class="s2">&quot;nfev&quot;</span><span class="p">:</span> <span class="n">_INT_ZERO</span><span class="p">,</span>
        <span class="s2">&quot;ngev&quot;</span><span class="p">:</span> <span class="n">_INT_ZERO</span><span class="p">,</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">is_run</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">state</span>

    <span class="n">delta1</span> <span class="o">=</span> <span class="mf">0.2</span>
    <span class="n">delta2</span> <span class="o">=</span> <span class="mf">0.1</span>
    <span class="n">maxiter</span> <span class="o">=</span> <span class="mi">10</span>  <span class="c1"># scipy: 10 jax: 30</span>
    <span class="k">while</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">])</span> <span class="ow">and</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;j&quot;</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">maxiter</span><span class="p">:</span>
        <span class="n">dalpha</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">]</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">])</span>
        <span class="n">b</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">])</span>

        <span class="n">cchk</span> <span class="o">=</span> <span class="n">delta1</span> <span class="o">*</span> <span class="n">dalpha</span>
        <span class="n">qchk</span> <span class="o">=</span> <span class="n">delta2</span> <span class="o">*</span> <span class="n">dalpha</span>

        <span class="n">a_j_cubic</span> <span class="o">=</span> <span class="n">_cubicmin</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">],</span>
                              <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">])</span>
        <span class="n">use_cubic</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;j&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="n">a_j_cubic</span><span class="p">)</span> <span class="ow">and</span> \
                    <span class="n">mnp</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">a_j_cubic</span> <span class="o">&gt;</span> <span class="n">a</span> <span class="o">+</span> <span class="n">cchk</span><span class="p">,</span> <span class="n">a_j_cubic</span> <span class="o">&lt;</span> <span class="n">b</span> <span class="o">-</span> <span class="n">cchk</span><span class="p">)</span>

        <span class="n">a_j_quad</span> <span class="o">=</span> <span class="n">_quadmin</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">],</span>
                            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">])</span>
        <span class="n">use_quad</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">use_cubic</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="n">a_j_quad</span><span class="p">)</span> <span class="ow">and</span> \
                   <span class="n">mnp</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">a_j_quad</span> <span class="o">&gt;</span> <span class="n">a</span> <span class="o">+</span> <span class="n">qchk</span><span class="p">,</span> <span class="n">a_j_quad</span> <span class="o">&lt;</span> <span class="n">b</span> <span class="o">-</span> <span class="n">qchk</span><span class="p">)</span>

        <span class="n">a_j_bisection</span> <span class="o">=</span> <span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">])</span> <span class="o">/</span> <span class="mf">2.0</span>
        <span class="n">use_bisection</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">use_cubic</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">use_quad</span><span class="p">)</span>

        <span class="n">a_j</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">use_cubic</span><span class="p">,</span> <span class="n">a_j_cubic</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">])</span>
        <span class="n">a_j</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">use_quad</span><span class="p">,</span> <span class="n">a_j_quad</span><span class="p">,</span> <span class="n">a_j</span><span class="p">)</span>
        <span class="n">a_j</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">use_bisection</span><span class="p">,</span> <span class="n">a_j_bisection</span><span class="p">,</span> <span class="n">a_j</span><span class="p">)</span>

        <span class="n">phi_j</span><span class="p">,</span> <span class="n">g_j</span><span class="p">,</span> <span class="n">dphi_j</span> <span class="o">=</span> <span class="n">fn</span><span class="p">(</span><span class="n">a_j</span><span class="p">)</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="n">j_to_high</span> <span class="o">=</span> <span class="p">(</span><span class="n">phi_j</span> <span class="o">&gt;</span> <span class="n">phi_0</span> <span class="o">+</span> <span class="n">c1</span> <span class="o">*</span> <span class="n">a_j</span> <span class="o">*</span> <span class="n">dphi_0</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">phi_j</span> <span class="o">&gt;=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_low&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">,</span> <span class="n">a_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">,</span> <span class="n">phi_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_high&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">,</span> <span class="n">dphi_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_high&quot;</span><span class="p">])</span>

        <span class="n">j_to_star</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">dphi_j</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="o">-</span><span class="n">c2</span> <span class="o">*</span> <span class="n">dphi_0</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">j_to_star</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_star</span><span class="p">,</span> <span class="n">a_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_star</span><span class="p">,</span> <span class="n">phi_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_star</span><span class="p">,</span> <span class="n">g_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_star</span><span class="p">,</span> <span class="n">dphi_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">])</span>

        <span class="n">low_to_high</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">j_to_star</span><span class="p">)</span> <span class="ow">and</span> \
                      <span class="n">dphi_j</span> <span class="o">*</span> <span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="mf">0.</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">low_to_high</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">low_to_high</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">low_to_high</span><span class="p">,</span> <span class="n">a_low</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_high&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">low_to_high</span><span class="p">,</span> <span class="n">phi_low</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_high&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_high&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">low_to_high</span><span class="p">,</span> <span class="n">dphi_low</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_high&quot;</span><span class="p">])</span>

        <span class="n">j_to_low</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">j_to_high</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">j_to_star</span><span class="p">)</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_low</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_rec&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_low</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_low&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_rec&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_low</span><span class="p">,</span> <span class="n">a_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_low&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_low&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_low</span><span class="p">,</span> <span class="n">phi_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_low&quot;</span><span class="p">])</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_low&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">j_to_low</span><span class="p">,</span> <span class="n">dphi_j</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_low&quot;</span><span class="p">])</span>

        <span class="c1"># next iteration</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;j&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;j&quot;</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>

    <span class="n">state</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;j&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">maxiter</span>
    <span class="k">return</span> <span class="n">state</span>


<span class="k">class</span> <span class="nc">LineSearch</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Line Search that satisfies strong Wolfe conditions.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Initialize LineSearch.&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LineSearch</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">func</span> <span class="o">=</span> <span class="n">func</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xk</span><span class="p">,</span> <span class="n">pk</span><span class="p">,</span> <span class="n">old_fval</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">old_old_fval</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">gfk</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                  <span class="n">c1</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span> <span class="n">c2</span><span class="o">=</span><span class="mf">0.9</span><span class="p">,</span> <span class="n">maxiter</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">fval_and_grad</span><span class="p">(</span><span class="n">alpha</span><span class="p">):</span>
            <span class="n">xkk</span> <span class="o">=</span> <span class="n">xk</span> <span class="o">+</span> <span class="n">alpha</span> <span class="o">*</span> <span class="n">pk</span>
            <span class="n">fkk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">(</span><span class="n">xkk</span><span class="p">)</span>
            <span class="n">gkk</span> <span class="o">=</span> <span class="n">grad</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">func</span><span class="p">)(</span><span class="n">xkk</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">fkk</span><span class="p">,</span> <span class="n">gkk</span><span class="p">,</span> <span class="n">mnp</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">gkk</span><span class="p">,</span> <span class="n">pk</span><span class="p">)</span>

        <span class="n">_FLOAT_ZERO</span> <span class="o">=</span> <span class="n">_to_tensor</span><span class="p">(</span><span class="mf">0.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">xk</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
        <span class="n">_FLOAT_ONE</span> <span class="o">=</span> <span class="n">_to_tensor</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">xk</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">old_fval</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">gfk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">nfev</span><span class="p">,</span> <span class="n">ngev</span> <span class="o">=</span> <span class="n">_INT_ONE</span><span class="p">,</span> <span class="n">_INT_ONE</span>
            <span class="n">phi_0</span><span class="p">,</span> <span class="n">g_0</span><span class="p">,</span> <span class="n">dphi_0</span> <span class="o">=</span> <span class="n">fval_and_grad</span><span class="p">(</span><span class="n">_FLOAT_ZERO</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">nfev</span><span class="p">,</span> <span class="n">ngev</span> <span class="o">=</span> <span class="n">_INT_ZERO</span><span class="p">,</span> <span class="n">_INT_ZERO</span>
            <span class="n">phi_0</span><span class="p">,</span> <span class="n">g_0</span> <span class="o">=</span> <span class="n">old_fval</span><span class="p">,</span> <span class="n">gfk</span>
            <span class="n">dphi_0</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">g_0</span><span class="p">,</span> <span class="n">pk</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">old_old_fval</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">start_value</span> <span class="o">=</span> <span class="n">_FLOAT_ONE</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">old_phi0</span> <span class="o">=</span> <span class="n">old_old_fval</span>
            <span class="n">candidate_start_value</span> <span class="o">=</span> <span class="mf">1.01</span> <span class="o">*</span> <span class="mi">2</span> <span class="o">*</span> <span class="p">(</span><span class="n">phi_0</span> <span class="o">-</span> <span class="n">old_phi0</span><span class="p">)</span> <span class="o">/</span> <span class="n">dphi_0</span>
            <span class="n">start_value</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span>
                <span class="n">mnp</span><span class="o">.</span><span class="n">isfinite</span><span class="p">(</span><span class="n">candidate_start_value</span><span class="p">),</span>
                <span class="n">mnp</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">candidate_start_value</span><span class="p">,</span> <span class="n">_FLOAT_ONE</span><span class="p">),</span>
                <span class="n">_FLOAT_ONE</span>
            <span class="p">)</span>

        <span class="n">state</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;done&quot;</span><span class="p">:</span> <span class="n">_BOOL_FALSE</span><span class="p">,</span>
            <span class="s2">&quot;failed&quot;</span><span class="p">:</span> <span class="n">_BOOL_FALSE</span><span class="p">,</span>
            <span class="s2">&quot;i&quot;</span><span class="p">:</span> <span class="n">_INT_ONE</span><span class="p">,</span>
            <span class="s2">&quot;a_i&quot;</span><span class="p">:</span> <span class="n">_FLOAT_ZERO</span><span class="p">,</span>
            <span class="s2">&quot;phi_i&quot;</span><span class="p">:</span> <span class="n">phi_0</span><span class="p">,</span>
            <span class="s2">&quot;dphi_i&quot;</span><span class="p">:</span> <span class="n">dphi_0</span><span class="p">,</span>
            <span class="s2">&quot;nfev&quot;</span><span class="p">:</span> <span class="n">nfev</span><span class="p">,</span>
            <span class="s2">&quot;ngev&quot;</span><span class="p">:</span> <span class="n">ngev</span><span class="p">,</span>
            <span class="s2">&quot;a_star&quot;</span><span class="p">:</span> <span class="n">_FLOAT_ZERO</span><span class="p">,</span>
            <span class="s2">&quot;phi_star&quot;</span><span class="p">:</span> <span class="n">phi_0</span><span class="p">,</span>
            <span class="s2">&quot;dphi_star&quot;</span><span class="p">:</span> <span class="n">dphi_0</span><span class="p">,</span>
            <span class="s2">&quot;g_star&quot;</span><span class="p">:</span> <span class="n">g_0</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="k">while</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">])</span> <span class="ow">and</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="n">maxiter</span><span class="p">:</span>
            <span class="n">a_i</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_i&quot;</span><span class="p">]</span> <span class="o">*</span> <span class="mf">2.0</span><span class="p">,</span> <span class="n">start_value</span><span class="p">)</span>
            <span class="n">phi_i</span><span class="p">,</span> <span class="n">g_i</span><span class="p">,</span> <span class="n">dphi_i</span> <span class="o">=</span> <span class="n">fval_and_grad</span><span class="p">(</span><span class="n">a_i</span><span class="p">)</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>

            <span class="c1"># Armijo condition</span>
            <span class="n">cond1</span> <span class="o">=</span> <span class="p">(</span><span class="n">phi_i</span> <span class="o">&gt;</span> <span class="n">phi_0</span> <span class="o">+</span> <span class="n">c1</span> <span class="o">*</span> <span class="n">a_i</span> <span class="o">*</span> <span class="n">dphi_0</span><span class="p">)</span> <span class="ow">or</span> \
                    <span class="p">(</span><span class="n">phi_i</span> <span class="o">&gt;=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_i&quot;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">zoom1</span> <span class="o">=</span> <span class="n">_zoom</span><span class="p">(</span><span class="n">fval_and_grad</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_i&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_i&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_i&quot;</span><span class="p">],</span>
                          <span class="n">a_i</span><span class="p">,</span> <span class="n">phi_i</span><span class="p">,</span> <span class="n">dphi_i</span><span class="p">,</span> <span class="n">phi_0</span><span class="p">,</span> <span class="n">g_0</span><span class="p">,</span> <span class="n">dphi_0</span><span class="p">,</span> <span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">cond1</span><span class="p">)</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cond1</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cond1</span> <span class="ow">and</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">]</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond1</span><span class="p">,</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond1</span><span class="p">,</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond1</span><span class="p">,</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond1</span><span class="p">,</span> <span class="n">zoom1</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">])</span>

            <span class="c1"># curvature condition</span>
            <span class="n">cond2</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">cond1</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">dphi_i</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="o">-</span><span class="n">c2</span> <span class="o">*</span> <span class="n">dphi_0</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="n">cond2</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond2</span><span class="p">,</span> <span class="n">a_i</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond2</span><span class="p">,</span> <span class="n">phi_i</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond2</span><span class="p">,</span> <span class="n">g_i</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond2</span><span class="p">,</span> <span class="n">dphi_i</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">])</span>

            <span class="c1"># satisfying the strong wolf conditions</span>
            <span class="n">cond3</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">cond1</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mnp</span><span class="o">.</span><span class="n">logical_not</span><span class="p">(</span><span class="n">cond2</span><span class="p">)</span> <span class="ow">and</span> <span class="n">dphi_i</span> <span class="o">&gt;=</span> <span class="mf">0.</span>
            <span class="n">zoom2</span> <span class="o">=</span> <span class="n">_zoom</span><span class="p">(</span><span class="n">fval_and_grad</span><span class="p">,</span> <span class="n">a_i</span><span class="p">,</span> <span class="n">phi_i</span><span class="p">,</span> <span class="n">dphi_i</span><span class="p">,</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_i&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_i&quot;</span><span class="p">],</span>
                          <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_i&quot;</span><span class="p">],</span> <span class="n">phi_0</span><span class="p">,</span> <span class="n">g_0</span><span class="p">,</span> <span class="n">dphi_0</span><span class="p">,</span> <span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">cond3</span><span class="p">)</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="n">cond3</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="p">(</span><span class="n">cond3</span> <span class="ow">and</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond3</span><span class="p">,</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond3</span><span class="p">,</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond3</span><span class="p">,</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">])</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">cond3</span><span class="p">,</span> <span class="n">zoom2</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">],</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_star&quot;</span><span class="p">])</span>

            <span class="c1"># next iteration</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_i&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">a_i</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_i&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">phi_i</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;dphi_i&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dphi_i</span>

        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;status&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">],</span>
            <span class="mi">1</span><span class="p">,</span>  <span class="c1"># zoom failed</span>
            <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span>
                <span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="n">maxiter</span><span class="p">,</span>
                <span class="mi">3</span><span class="p">,</span>  <span class="c1"># maxiter reached</span>
                <span class="mi">0</span><span class="p">,</span>  <span class="c1"># passed (should be)</span>
            <span class="p">),</span>
        <span class="p">)</span>
        <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">mnp</span><span class="o">.</span><span class="n">where</span><span class="p">(</span>
            <span class="n">_to_tensor</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">dtype</span> <span class="o">!=</span> <span class="n">mstype</span><span class="o">.</span><span class="n">float64</span><span class="p">)</span>
            <span class="ow">and</span> <span class="p">(</span><span class="n">mnp</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">])</span> <span class="o">&lt;</span> <span class="mf">1e-8</span><span class="p">),</span>
            <span class="n">mnp</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">])</span> <span class="o">*</span> <span class="mf">1e-8</span><span class="p">,</span>
            <span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">],</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">state</span>


<div class="viewcode-block" id="line_search"><a class="viewcode-back" href="../../../../api_python/scipy/mindspore.scipy.optimize.line_search.html#mindspore.scipy.optimize.line_search">[docs]</a><span class="k">def</span> <span class="nf">line_search</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">xk</span><span class="p">,</span> <span class="n">pk</span><span class="p">,</span> <span class="n">gfk</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">old_fval</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">old_old_fval</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">c1</span><span class="o">=</span><span class="mf">1e-4</span><span class="p">,</span>
                <span class="n">c2</span><span class="o">=</span><span class="mf">0.9</span><span class="p">,</span> <span class="n">maxiter</span><span class="o">=</span><span class="mi">20</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Inexact line search that satisfies strong Wolfe conditions.</span>

<span class="sd">    Algorithm 3.5 from Wright and Nocedal, &#39;Numerical Optimization&#39;, 1999, pg. 59-61</span>

<span class="sd">    Args:</span>
<span class="sd">        f (function): function of the form f(x) where x is a flat Tensor and returns a real</span>
<span class="sd">            scalar. The function should be composed of operations with vjp defined.</span>
<span class="sd">        xk (Tensor): initial guess.</span>
<span class="sd">        pk (Tensor): direction to search in. Assumes the direction is a descent direction.</span>
<span class="sd">        gfk (Tensor): initial value of value_and_gradient as position. Default: None.</span>
<span class="sd">        old_fval (Tensor): The same as `gfk`. Default: None.</span>
<span class="sd">        old_old_fval (Tensor): unused argument, only for scipy API compliance. Default: None.</span>
<span class="sd">        c1 (float): Wolfe criteria constant, see ref. Default: 1e-4.</span>
<span class="sd">        c2 (float): The same as `c1`. Default: 0.9.</span>
<span class="sd">        maxiter (int): maximum number of iterations to search. Default: 20.</span>

<span class="sd">    Returns:</span>
<span class="sd">        LineSearchResults, results of line search results.</span>

<span class="sd">    Supported Platforms:</span>
<span class="sd">        ``CPU`` ``GPU``</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; import numpy as onp</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.scipy.optimize import line_search</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.common import Tensor</span>
<span class="sd">        &gt;&gt;&gt; x0 = Tensor(onp.ones(2).astype(onp.float32))</span>
<span class="sd">        &gt;&gt;&gt; p0 = Tensor(onp.array([-1, -1]).astype(onp.float32))</span>
<span class="sd">        &gt;&gt;&gt; def func(x):</span>
<span class="sd">        &gt;&gt;&gt;     return x[0] ** 2 - x[1] ** 3</span>
<span class="sd">        &gt;&gt;&gt; res = line_search(func, x0, p0)</span>
<span class="sd">        &gt;&gt;&gt; res.a_k</span>
<span class="sd">        1.0</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">state</span> <span class="o">=</span> <span class="n">LineSearch</span><span class="p">(</span><span class="n">f</span><span class="p">)(</span><span class="n">xk</span><span class="p">,</span> <span class="n">pk</span><span class="p">,</span> <span class="n">old_fval</span><span class="p">,</span> <span class="n">old_old_fval</span><span class="p">,</span> <span class="n">gfk</span><span class="p">,</span> <span class="n">c1</span><span class="p">,</span> <span class="n">c2</span><span class="p">,</span> <span class="n">maxiter</span><span class="p">)</span>
    <span class="c1"># If running in graph mode, the state is a tuple.</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">state</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
        <span class="n">state</span> <span class="o">=</span> <span class="n">_LineSearchResults</span><span class="p">(</span><span class="n">failed</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">state</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span>
                                   <span class="n">nit</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span>
                                   <span class="n">nfev</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">6</span><span class="p">]),</span>
                                   <span class="n">ngev</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">7</span><span class="p">]),</span>
                                   <span class="n">k</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">2</span><span class="p">]),</span>
                                   <span class="n">a_k</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">8</span><span class="p">]),</span>
                                   <span class="n">f_k</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">9</span><span class="p">]),</span>
                                   <span class="n">g_k</span><span class="o">=</span><span class="n">state</span><span class="p">[</span><span class="mi">11</span><span class="p">],</span>
                                   <span class="n">status</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="mi">12</span><span class="p">]))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">state</span> <span class="o">=</span> <span class="n">_LineSearchResults</span><span class="p">(</span><span class="n">failed</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;failed&quot;</span><span class="p">]</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">state</span><span class="p">[</span><span class="s2">&quot;done&quot;</span><span class="p">]),</span>
                                   <span class="n">nit</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span>
                                   <span class="n">nfev</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;nfev&quot;</span><span class="p">]),</span>
                                   <span class="n">ngev</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;ngev&quot;</span><span class="p">]),</span>
                                   <span class="n">k</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;i&quot;</span><span class="p">]),</span>
                                   <span class="n">a_k</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;a_star&quot;</span><span class="p">]),</span>
                                   <span class="n">f_k</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;phi_star&quot;</span><span class="p">]),</span>
                                   <span class="n">g_k</span><span class="o">=</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;g_star&quot;</span><span class="p">],</span>
                                   <span class="n">status</span><span class="o">=</span><span class="n">_to_scalar</span><span class="p">(</span><span class="n">state</span><span class="p">[</span><span class="s2">&quot;status&quot;</span><span class="p">]))</span>

    <span class="k">return</span> <span class="n">state</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>